package com.example.mapper;

import com.example.domain.Users;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class UserMapper {
    public static void main(String[] args) {
//        Users user=new UserMapper().findUserById(1);
//        System.out.println(user.getUserName());

        //插入用户测试
        Users user=new Users();
        user.setUserId(2913);
        user.setPassword("123456");
        user.setUserSex(1);
        user.setUserName("王六四");
       int rows=new UserMapper().insertUser(user);
        System.out.println("rows:"+rows);

        //修改用户测试
//        Users user=new Users();
//        user.setUserId(2900);
//        user.setPassword("123456");
//        user.setUserSex(1);
//        user.setUserName("大科学家");
//        int rows=new UserMapper().updateUser(user);
//        System.out.println("rows:"+rows);

        //删除用户测试
//        Users user=new Users();
//        user.setUserId(46);
//        int rows=new UserMapper().delUser(user);
//        System.out.println("rows:"+rows);
    }

    //查询User
    public Users findUserById(Integer id) throws Exception{
        Users user = null;

        InputStream inputStream = null;
        SqlSession sqlSession=null;

        String resource = "mybatis-config.xml";
        inputStream = Resources.getResourceAsStream(resource);

        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        sqlSession=sqlSessionFactory.openSession();

        user=sqlSession.selectOne("test.selectUserById",id);

        //user=sqlSession.selectOne("test.selectUserById","李四";

        return user;
    }


    //修改User


    //新增User
    public int insertUser(Users users){
        int rows=0;
        String resource = "mybatis-config.xml";
        InputStream inputStream = null;
        SqlSession sqlSession=null;
        try {
            inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            sqlSession=sqlSessionFactory.openSession();
            rows=sqlSession.insert("test.insertUser",users);
            sqlSession.commit();//注意必须提交
        } catch (IOException e) {
            sqlSession.rollback();
            e.printStackTrace();
        }
        return rows;
    }


    //修改User
    public int updateUser(Users users){
        int rows=0;
        String resource = "mybatis-config.xml";
        InputStream inputStream = null;
        SqlSession sqlSession=null;
        try {
            inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            sqlSession=sqlSessionFactory.openSession();
            rows=sqlSession.insert("test.updateUser",users);
            sqlSession.commit();//注意必须提交
        } catch (IOException e) {
            sqlSession.rollback();
            e.printStackTrace();
        }
        return rows;
    }

    //删除User
    public int delUser(Users user){
        int rows=0;
        String resource = "mybatis-config.xml";
        InputStream inputStream = null;
        SqlSession sqlSession=null;
        try {
            inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            sqlSession=sqlSessionFactory.openSession();
            rows=sqlSession.insert("test.delUser",user);
            sqlSession.commit();//注意必须提交
        } catch (IOException e) {
            sqlSession.rollback();
            e.printStackTrace();
        }
        return rows;
    }


}
